package com.leesche.zcsp.node.web.controller.web.v2.param;

import com.leesche.zcsp.node.utils.web.PagingResult;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.model.PubDevSetParam;
import com.leesche.zcsp.node.web.service.PubDevSetParamService;
import com.leesche.zcsp.node.web.service.impl.data.read.PubDevSetParamDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDevSetParamCondition;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017-01-05.
 */
@Controller
@RequestMapping("/v2/devParam")
public class WebDevParamContoller {
    private final static Logger LOGGER = LoggerFactory.getLogger(WebDevParamContoller.class);

    private static  final String SETPARAM_MAINVIEW="";

    @Autowired
    PrivilegeService privilegeService;

    @Autowired
    PubDevSetParamService pubDevSetParamService;


    /***
     * <p>参数组主界面显示</[p>
     * @param model
     * @param condition
     * @return
     */
    @RequestMapping(value = "/showView", method = RequestMethod.GET)
    public String showView(Model model, QueryPubDevSetParamCondition condition
    ) {
        return SETPARAM_MAINVIEW;
    }

    /**
     * 根据条件分页查询
     * @param condition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list.json", method = RequestMethod.POST)
    public Map<String,Object> findByPage(QueryPubDevSetParamCondition condition){
        try{
            PagingResult<PubDevSetParamDto> pagingResult= pubDevSetParamService.findAllByPage(condition);
            return  WebResult.buildOf(pagingResult).toMap();
        }catch (Exception e){
            LOGGER.error("PubDevSetParam find by page" ,e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 根据条件查询所有的
     * @param condition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findAll", method = RequestMethod.POST)
    public Map<String,Object> findBAll(QueryPubDevSetParamCondition condition){
        try{
            List<PubDevSetParamDto> lists= pubDevSetParamService.findListsByCondition(condition);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("lists", lists);
            return WebResult.success().put("data",map).toMap();
        }catch (Exception e){
            LOGGER.error("PubDevSetParam find by page" ,e);
            return WebResult.failure(e).toMap();
        }
    }




    /***
     * <p>增加设备参数</p>
     * @param devSetParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addDevParam", method = RequestMethod.POST)
    public Map<String,Object> addDevParam(PubDevSetParam devSetParam){
        devSetParam.setCreatetime(new Date());
        devSetParam.setUpdatetime(new Date());
        devSetParam.setAdminid(privilegeService.getCurrentPrincipal().getUserid());

        try{
            pubDevSetParamService.insert(devSetParam);
            return WebResult.success().info("操作成功!").toMap();
        }catch (Exception e){
            LOGGER.error("PubParamGroup isnert",e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * <p>编辑设备类型</p>
     * @param devParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "editDevParam", method = RequestMethod.POST)
    public Map<String,Object> editDevParam(PubDevSetParam devParam){
        PubDevSetParam pubDevSetParam=pubDevSetParamService.selectByPrimaryKey(devParam.getDevparamsetid());
        if(pubDevSetParam.getAdminid()!=null&&!pubDevSetParam.getAdminid().equals(privilegeService.getCurrentPrincipal().getUserid())){
            return WebResult.failure("您无权限操作此参数组").toMap();
        }
        devParam.setCreatetime(new Date());
        devParam.setUpdatetime(new Date());
        devParam.setAdminid(privilegeService.getCurrentPrincipal().getUserid());

        try{
            pubDevSetParamService.updateByPrimaryKey(devParam);
            return WebResult.success().info("操作成功!").toMap();
        }catch (Exception e){
            LOGGER.error("PubParamGroup isnert",e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * <p>删除设备参数</p>
     * @param devParamId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteDevParam", method = RequestMethod.POST)
    public Map<String,Object> deleteDevParam(Long devParamId){
        PubDevSetParam pubDevSetParam=pubDevSetParamService.selectByPrimaryKey(devParamId);
        if(pubDevSetParam.getAdminid()!=null&&!pubDevSetParam.getAdminid().equals(privilegeService.getCurrentPrincipal().getUserid())){
            return WebResult.failure("您无权限操作此参数组").toMap();
        }
        try{
            pubDevSetParamService.deleteByPrimaryKey(devParamId);
            return WebResult.success().info("操作成功!").toMap();
        }catch (Exception e){
            LOGGER.error("pubDevSetParam isnert",e);
            return WebResult.failure(e).toMap();
        }
    }


}
